// 模拟的文物数据分类
const categories = ['所有', '青铜器', '陶瓷器', '书画', '玉器'];

// 图片和视频数据
const imageUrls2 = [
  'https://omo-oss-image.thefastimg.com/portal-saas/new2023031518242917793/cms/image/3a372965-37a6-469a-b252-489c5f04bbb7.jpg',
  'https://omo-oss-image.thefastimg.com/portal-saas/new2023031518242917793/cms/image/a0f9e903-186f-46a8-9d3c-1e0d02e63b22.jpg',
  // ... 其他图片路径
];

const titles = [
  '博物馆新展览开幕',
  '纪念日活动回顾',
  // ... 其他标题
];

// 随机生成日期
const getRandomDate = () => {
  const start = new Date(2024, 0, 1);
  const end = new Date(2024, 11, 31);
  const randomTime = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
  return randomTime.toISOString().split('T')[0];
};

// 动态数据列表
const dynamicList = [];

// 生成动态新闻数据
const generateDynamicNews = (count = 5) => {
  const result = [];
  for (let i = 0; i < count; i++) {
    const dynamicNews = {
      title: titles[Math.floor(Math.random() * titles.length)],
      date: getRandomDate(),
    };
    dynamicList.push(dynamicNews); // 保存到动态列表
    result.push(dynamicNews); // 返回的结果
  }
  return result;
};

// 获取动态新闻接口
const getDynamicNews = ({ page = 1, pageSize = 5 } = {}) => {
  // 确保数据存在
  if (dynamicList.length === 0) {
    generateDynamicNews(10); // 预先生成 50 条动态数据
  }

  // 分页处理
  const total = dynamicList.length;
  const startIndex = (page - 1) * pageSize;
  const paginatedData = dynamicList.slice(startIndex, startIndex + pageSize);

  return {
    data: paginatedData,
    total,
  };
};

// 文物数据初始化
const mockData = [];
for (let i = 1; i <= 50; i++) {
  mockData.push({
    id: i,
    title: `文物${i}`,
    description: `这是文物${i}的简短描述。`,
    category: categories[Math.floor(Math.random() * categories.length)],
    imageUrl: imageUrls2[Math.floor(Math.random() * imageUrls2.length)],
  });
}

// 获取文物数据并支持分页与过滤
const getArtifacts = ({ page = 1, pageSize = 10, category = '所有', query = '' } = {}) => {
  let filteredData = mockData;

  // 分类过滤
  if (category !== '所有') {
    filteredData = filteredData.filter((item) => item.category === category);
  }

  // 搜索过滤
  if (query) {
    filteredData = filteredData.filter((item) =>
      item.title.includes(query) || item.description.includes(query)
    );
  }

  // 分页处理
  const total = filteredData.length;
  const startIndex = (page - 1) * pageSize;
  return {
    data: filteredData.slice(startIndex, startIndex + pageSize),
    total,
  };
};

// 根据 ID 获取单条数据（文物或新闻）
const getItemById = (id) => {
  return mockData.find((item) => item.id === id) || null;
};

// 导出模块
module.exports = {
  getArtifacts,
  categories,
  generateDynamicNews,
  getDynamicNews, // 新增接口
  getItemById,
};
